package com.fita.entity.data;

import com.demo2do.core.format.PersistentFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import java.beans.Transient;
import java.util.Arrays;
import java.util.List;

/**
 * 国家
 * 
 * @author mohanlan
 */
@Entity
@Table(name = "country")
@PersistentFormat
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class Country {

    @Id
    @GeneratedValue
    private Long id;

    private String code;

    private String englishName;

    private String chineseName;

    private String suppliers;

    @Column(name = "is_disabled")
    private boolean disabled;

    /**
     * The default constructor
     */
    public Country() {}
    
    /**
     * 
     * @return
     */
    public boolean isValid() {
        return !this.disabled && StringUtils.isNotBlank(this.suppliers);
    }
    /**
     * 
     * @return
     */
    @Transient
    public boolean isAlamo() {
        if(StringUtils.isNotBlank(this.suppliers)) {
            String[] suppliers = StringUtils.split(this.suppliers, ",");
            List<String> result = Arrays.asList(suppliers);
            return result.contains("Alamo");
        } else {
            return false;
        }
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public boolean isEnterprise() {
        if(StringUtils.isNotBlank(this.suppliers)) {
            String[] suppliers = StringUtils.split(this.suppliers, ",");
            List<String> result = Arrays.asList(suppliers);
            return result.contains("Enterprise");
        } else {
            return false;
        }
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public String[] getSupplierCodes() {
        return StringUtils.split(this.suppliers,",");
    }
    
    /**
     * 
     * @return
     */
    public boolean isNational() {
        if(StringUtils.isNotBlank(this.suppliers)) {
            String[] suppliers = StringUtils.split(this.suppliers, ",");
            List<String> result = Arrays.asList(suppliers);
            return result.contains("National");
        } else {
            return false;
        }
    }
    
    /**
     * 
     * @return
     */
    public String getEnglishCode() {
        return this.englishName.substring(0, 1);
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the code
     */
    public String getCode() {
        return code;
    }

    /**
     * @return the englishName
     */
    public String getEnglishName() {
        return englishName;
    }

    /**
     * @return the chineseName
     */
    public String getChineseName() {
        return chineseName;
    }

    /**
     * @return the suppliers
     */
    public String getSuppliers() {
        return suppliers;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param code the code to set
     */
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * @param englishName the englishName to set
     */
    public void setEnglishName(String englishName) {
        this.englishName = englishName;
    }

    /**
     * @param chineseName the chineseName to set
     */
    public void setChineseName(String chineseName) {
        this.chineseName = chineseName;
    }

    /**
     * @param suppliers the suppliers to set
     */
    public void setSuppliers(String suppliers) {
        this.suppliers = suppliers;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
}
